This set of scripts are intended for the priors and flat versions of the PREMUP paradigm. We need to select below here which version to look at.

Compute individual performance indeces

# Initialize
strong_acc <- matrix(data=NA, nrow=40*4, ncol=max(exp.p$which_sub))
weak_acc <-  matrix(data=NA, nrow=40*4, ncol=max(exp.p$which_sub))
flat_acc <-  matrix(data=NA, nrow=40*4, ncol=max(exp.p$which_sub))
# temp <- NA
# Initialize plot
# par(mfrow=c(exp.p$nSubs/2,4), oma=c(0,0,0,0)) # Subplots
# Loop over subjects
for(cSub in exp.p$which_sub){
  
  # Get subject code
  sub_code <- paste("sub-", sprintf("%03d", cSub), sep="")

  # Get one subject's data
  sub_data <- subset(x = learning, subset = participant == cSub) # Data
  load(paste(cleaned_dir, "../../data/BIDS/", sub_code,"/", sub_code, "_params.RData", sep="")) # Parameters
  
  # Get cummulative accuracy split by scenes
  for (cScn in seq(1,6, by=2)){
    cumm_acc <- cumsum(sub_data$trial_acc_manual[sub_data$scn_cat==cScn])
    cumm_acc2 <- cumsum(sub_data$trial_acc_manual[sub_data$scn_cat==cScn+1])
    cumm_acc <- cumm_acc / c(1:length(cumm_acc)) # Cummulative accuracy
    cumm_acc2 <- cumm_acc2 / c(1:length(cumm_acc2)) # Cummulative accuracy
   
    # Get current scene's info
    curr_scn_cond <- unique(sub_data$scn_condition[sub_data$scn_cat==cScn])
    curr_scn_cond_label <- p$scn_cont[curr_scn_cond]
    
    # Store average perfromance
    if(curr_scn_cond_label==0.9){
      strong_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }else if(curr_scn_cond_label==0.7) {
      weak_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }else{
      flat_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }
    
    # Plot
    plot(cumm_acc, ylim = c(0,1), xlim = c(0,160),xaxt="n", col="pink",type="n",
         main = paste(curr_scn_cond_label, "contingency"))
    text(labels=sub_code, x=0, y=1.2, xpd=T)
    abline(h=.5, col = "red", lty = 2); abline(h=curr_scn_cond_label ,col = "green")
    abline(v=0, col = "black", lty = 4);abline(v=40, col = "black", lty = 4); abline(v=80, col = "black", lty = 4);
    abline(v=120, col = "black", lty = 4);abline(v=160, col = "black", lty = 4);
    xtick<-seq(0, 160, by=40)
    axis(side=1, at=xtick, labels = FALSE)
    text(x=xtick,  par("usr")[3],
    labels = c("1st", "2nd", "3rd", "4th", "hard-limit"), pos = 1, xpd = TRUE)
    points(seq(1, length(cumm_acc2)), cumm_acc2, col = "blue")
  }
}

Compute group level performance

# Overall performance
group_perf <- data.frame(
                         trial = c(1:160),
                         strong_scn = rowMeans(strong_acc, na.rm = T),
                         weak_scn = rowMeans(weak_acc, na.rm = T),
                         flat_scn = rowMeans(flat_acc, na.rm = T),
                         strong_sd = rowSds(strong_acc, na.rm = T),
                         weak_sd = rowSds(weak_acc, na.rm = T),
                         flat_sd = rowSds(flat_acc, na.rm = T)
)

Plot

par(mfrow=c(1,3)) # Subplots

for(cCond in 1:3){
  
  if(cCond == 1){ # Strong prior
      plot(group_perf$strong_scn, ylim = c(0,1), xlim = c(0,160),xaxt="n", col="blue",
           main = "Strong prior")
      abline(h=max(p$scn_cont) ,col = "green")
      
      # SD
    xx <- group_perf$strong_scn+group_perf$strong_sd
    yy <- group_perf$strong_scn-group_perf$strong_sd
  
  } else if(cCond ==2){# Weak prior
    plot(group_perf$weak_scn, ylim = c(0,1), xlim = c(0,160),xaxt="n", col="blue",
         main = "Weak prior")
    abline(h=p$scn_cont[2] ,col = "green")
    
    # SD
    xx <- group_perf$weak_scn+group_perf$weak_sd
    yy <- group_perf$weak_scn-group_perf$weak_sd
  } else {# Flat prior
    plot(group_perf$flat_scn, ylim = c(0,1), xlim = c(0,160),xaxt="n", col="blue",
         main = "Flat prior")
    abline(h=min(p$scn_cont) ,col = "green")
    
    # SD
    xx <- group_perf$flat_scn+group_perf$flat_sd
    yy <- group_perf$flat_scn-group_perf$flat_sd
  }
  
  # Format
  abline(h=.5, col = "red", lty = 2); 
  abline(v=0, col = "black", lty = 4);abline(v=40, col = "black", lty = 4); abline(v=80, col = "black", lty = 4); 
  abline(v=120, col = "black", lty = 4);abline(v=160, col = "black", lty = 4);
  xtick<-seq(0, 160, by=40)
  axis(side=1, at=xtick, labels = FALSE)
  text(x=xtick,  par("usr")[3],
  labels = c("1st", "2nd", "3rd", "4th", "hard-limit"), pos = 1, xpd = TRUE)
  
  polygon(x= c(1:160, 160:1), y = c((xx),rev(yy)), col = rgb(.5,.5,.8,.3), border = "red")
}

# Last iteration performance
last_it <- learning[FALSE,]
last_strong_acc <- matrix(data=NA, nrow=40, ncol=max(exp.p$which_sub))
last_weak_acc <- matrix(data=NA, nrow=40, ncol=max(exp.p$which_sub))
last_flat_acc <- matrix(data=NA, nrow=40, ncol=max(exp.p$which_sub))
for(cSub in exp.p$which_sub){
  sub_last_it <- with(learning, subset(x=learning,
                    subset = 
                      participant == cSub &
                      iteration_index == exp.p$nIterations[exp.p$nIterations$participant==cSub,2]))
  sub_code <- paste("sub-", sprintf("%03d", cSub), sep="")
   load(paste(cleaned_dir, "../../data/BIDS/", sub_code,"/", sub_code, "_params.RData", sep=""))
  # Store
  last_it <- rbind(last_it, sub_last_it)
    
  for (cScn in seq(1,6, by=2)){
    cumm_acc <- cumsum(sub_last_it$trial_acc_manual[sub_last_it$scn_cat==cScn])
    cumm_acc2 <- cumsum(sub_last_it$trial_acc_manual[sub_last_it$scn_cat==cScn+1])
    cumm_acc <- cumm_acc / c(1:length(cumm_acc)) # Cummulative accuracy
    cumm_acc2 <- cumm_acc2 / c(1:length(cumm_acc2)) # Cummulative accuracy
   
    # Get current scene's info
    curr_scn_cond <- unique(sub_last_it$scn_condition[sub_last_it$scn_cat==cScn])
    curr_scn_cond_label <- p$scn_cont[curr_scn_cond]
    
    # Store perfromance
    if(curr_scn_cond_label==0.9){
      last_strong_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }else if(curr_scn_cond_label==0.7){
      last_weak_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }else {
      last_flat_acc[1:length(cumm_acc),cSub] <- rowMeans(cbind(cumm_acc, cumm_acc2))
    }
  }
}

# Overall performance
group_perf_last <- data.frame(
                         trial = c(1:40),
                         strong_scn = rowMeans(last_strong_acc, na.rm = T),
                         weak_scn = rowMeans(last_weak_acc, na.rm = T),
                         flat_scn = rowMeans(last_flat_acc, na.rm = T),
                         strong_sd = rowSds(last_strong_acc, na.rm = T),
                         weak_sd = rowSds(last_weak_acc, na.rm = T),
                         flat_sd = rowSds(last_flat_acc, na.rm = T)
)

Plot last iteration

par(mfrow=c(1,3), oma = c(0, 0, 0, 0)) # Subplots

for(cCond in 1:3){
  
  if( cCond == 1){ # Strong prior
    plot(group_perf_last$strong_scn, ylim = c(0,1), xlim = c(0,40),xaxt="n", col="blue",
           main = "Strong prior", ylab="cumm-acc")
    abline(h=max(p$scn_cont) ,col = "green")
    
       # SD
  xx <- group_perf_last$strong_scn+group_perf_last$strong_sd
  yy <- group_perf_last$strong_scn-group_perf_last$strong_sd
  
  } else if (cCond==2) {# Weak prior
    plot(group_perf_last$weak_scn, ylim = c(0,1), xlim = c(0,40),xaxt="n", col="blue",
         main = "Weak prior", ylab="cumm-acc")
    abline(h=p$scn_cont[2] ,col = "green")
    
     # SD
    xx <- group_perf_last$weak_scn+group_perf_last$weak_sd
    yy <- group_perf_last$weak_scn-group_perf_last$weak_sd
  
  }else {# Flat prior
    plot(group_perf_last$flat_scn, ylim = c(0,1), xlim = c(0,40),xaxt="n", col="blue",
         main = "Flat prior")
    abline(h=min(p$scn_cont) ,col = "green")
    
    # SD
    xx <- group_perf_last$flat_scn+group_perf_last$flat_sd
    yy <- group_perf_last$flat_scn-group_perf_last$flat_sd
  
  }
    
  # Format
  abline(h=.5, col = "red", lty = 2); 
  #xtick<-seq(0, 40, by=10)
  #axis(side=1, at=xtick, labels = FALSE)
  text(x=xtick,  par("usr")[3],)
  # labels = c("Last iteration"), pos = 1, xpd = TRUE)
  polygon(x= c(1:40, 40:1), y = c((xx),rev(yy)), col = rgb(.5,.5,.8,.3), border = "red")
  
}

Save cleaned data

#write.csv(learning_data, file = paste(output_dir, sub_code, "_task-learning_cleaned.csv", sep=""))
#write.csv(rating_data, file = paste(output_dir, sub_code, "_task-rating_cleaned.csv", sep=""))